<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	//$activetourl=model('jy_weishop/activity')->activetourl($_RQ['id']);
	if($activetourl){
		//exi('','',$activetourl);
	}
	$return=model('jy_weishop/goods_item')->get_goodsitem($return);
	$return['goods']['api_data']=json_decode($return['goods']['api_data'],true);
	if($_RQ['fromuser']){
		model('jy_weishop/fenxiao')->up_fenfee($return['shopset'],$return['goods']);
	}
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('mobile','avatar','nickname'));
	$keywords=$return['goods']['share_keywords']?:$return['goods']['title'].','.$return['sharedata']['desc'];
	$desc=$return['sharedata']['desc'];
	set_html('title',$return['goods']['title']?$return['goods']['title'].'-'.$return['shopset']['title']:'商品详情');
	set_html('keywords',$keywords);
	set_html('description',$desc);
	exi($return);
}

if($_RQ['op']=='addtocart'){
	if(!$_SESSION['uid']){
		model('jy_weishop/member')->checklogin();
	}
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	$isjson=intval($post['isjson']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$id)));
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$goods['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}else{
		$data['total']=$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['merchid']=$goods['merchid']?:$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
	}
	if($isjson){
		exi('success');
	}else{
		exi('成功加入购物车！');
	}
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	$store_id=$this->session('store_id');
	if(!$id){
		exi('请先选择商品！','error');
	}
	if($post['storeid']){
		
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$optionsid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']),'id');
	//默认商品规格价格：
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods=model('jy_weishop/activity')->get_upgoods($goods);
	$return['options']=$goods['options'];
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	if($post['isdish']){
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}else{
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='like'){
	if(!$_SESSION['uid']){
		model('jy_weishop/member')->checklogin();
	}
	$id = $_SESSION['goodsid'];
	if(!$id){
		$post=get_postdata();
	}
	$like=pdo_get('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_member_like',$insert);
		exi('关注成功');
	}	
}
if($_RQ['op']=='check_buy'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	if(!$goodsid){
		exi('请选择商品！','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),array('id','title','no_onebuy'));
	if($goods['no_onebuy']){
		exi('该商品，不能单独购买！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$goodsid)));
	model('jy_weishop/goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	exi('','','jy_mjicai/order.create//goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=model('jy_weishop/goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='getactive'){
	$goods_id=intval($_RQ['goods_id']);
	$active_id=intval($_RQ['active_id']);
	$active_plugin=$_RQ['active_plugin'];
	if($active_plugin && $goods_id){
		$active=model('jy_weishop/activity')->get_active($goods_id,$active_id,$active_plugin);
	}
	$return['active']=$active;
	exi($return);
}